﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VPLink2013.Framework.Extensions
{
    public static class SPWebExtension
    {

        public static bool SetWelcomePage(this SPWeb web, string welcomePagePath)
        {
            bool successed = false;
            PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
            if (pWeb != null)
            {
                try
                {
                    PublishingPageCollection pPages = pWeb.GetPublishingPages();
                    if (pPages != null)
                    {
                        //Set default page before to create new welcomepage
                        SPFile defaultHomePageFile = web.GetFile(welcomePagePath);
                        if (defaultHomePageFile != null)
                        {
                            web.RootFolder.WelcomePage = defaultHomePageFile.Url;
                            pWeb.DefaultPage = defaultHomePageFile;
                            web.RootFolder.Update();
                            web.Update();
                            pWeb.Update();
                            successed = true;
                        }
                    }
                }
                catch (Exception ex)
                {

                }
            }
            return successed;
        }
        public static void DeletePageIfExists(this SPWeb web, string pageName)
        {
            try
            {
                PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
                if (pWeb != null)
                {
                    PublishingPageCollection pPages = pWeb.GetPublishingPages();
                    if (pPages != null)
                    {
                        PublishingPage pageToDelete = pPages.ToList().Find(x => x.Name.Equals(pageName, StringComparison.CurrentCultureIgnoreCase));
                        if (pageToDelete != null)
                        {
                            if (pageToDelete.ListItem != null)
                            {
                                if (pageToDelete.ListItem.File != null)
                                {
                                    if (pageToDelete.ListItem.File.CheckOutType != SPFile.SPCheckOutType.None)
                                        pageToDelete.ListItem.File.CheckIn(string.Empty);

                                    pageToDelete.ListItem.File.Delete();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex) { }
        }

        public static PageLayout GetPageLayoutByName(this PageLayoutCollection pageLayouts, string pageLayoutName)
        {
            PageLayout pageLayout = null;
            try
            {
                pageLayout = pageLayouts[string.Format("/_catalogs/masterpage/{0}", pageLayoutName)];
            }
            catch (Exception ex) { }
            return pageLayout;
        }


        public static string AddNewPageWithLayout(this SPWeb web, PageLayout pageLayout, string pageName, string pageTitle)
        {
            string pageFilePath = string.Empty;
            PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
            if (pWeb != null)
            {
                PublishingPageCollection pPages = pWeb.GetPublishingPages();
                if (pPages != null)
                {
                    PublishingPage pPage = pPages.Add(pageName, pageLayout);
                    if (pPage != null)
                    {
                        SPListItem pageItem = pPage.ListItem;
                        if (pageItem != null)
                        {
                            pageItem["Title"] = pageTitle;
                            pageItem.Update();
                            pageItem.File.CheckIn(string.Format("Page de {0} crée automatiquement lors du déployment", pageTitle));
                            pageItem.File.Publish(string.Format("Page de {0} publiée automatiquement lors du déployment", pageTitle));
                            pageFilePath = pageItem.File.ServerRelativeUrl;
                        }
                    }
                }
            }
            return pageFilePath;
        }
    }
}
